﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class LayWaterTankCommands : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			Result result;
			try
			{
				if (commandData == null)
				{
					throw new System.ArgumentNullException();
				}
				UIApplication application = commandData.Application;
				UIDocument activeUIDocument = application.ActiveUIDocument;
				Document document = activeUIDocument.Document;
				Selection selection = activeUIDocument.Selection;
				List<ViewType> list = new List<ViewType>();
                list.Add((ViewType)1);
                list.Add((ViewType)2);
                list.Add((ViewType)4);
				if (!YJKRevitTools.ViewCheck(application.ActiveUIDocument, list))
				{
					result = Autodesk.Revit.UI.Result.Cancelled;
				}
				else
				{
					for (;;)
					{
						HYEquipmentCommon hyequipmentCommon = new HYEquipmentCommon();
						ICollection<ElementId> allFamilyInstance = hyequipmentCommon.GetAllFamilyInstance(document, BuiltInCategory.OST_MechanicalEquipment);
						SpaceFamilyDataReader spaceFamilyDataReader = new SpaceFamilyDataReader();
						FormLayWaterTankNew formLayWaterTankNew = new FormLayWaterTankNew(document);
						DialogResult dialogResult = formLayWaterTankNew.ShowDialog();
						if (dialogResult == DialogResult.OK)
						{
							ListItemWaterTankPara currentTankPara = formLayWaterTankNew.currentTankPara;
							FamilySymbol familySymbol = formLayWaterTankNew.fs;
							if (familySymbol == null)
							{
								familySymbol = spaceFamilyDataReader.GetFamilySymbolById(int.Parse(formLayWaterTankNew.currentTankPara.id), "WaterTank", document, BuiltInCategory.OST_MechanicalEquipment);
							}
							if (familySymbol == null)
							{
								YJKMessageBox.Information("没有获取到该型号的族");
							}
							else
							{
								try
								{
									activeUIDocument.PromptForFamilyInstancePlacement(familySymbol);
								}
								catch (Exception)
								{
								}
								ICollection<ElementId> allFamilyInstance2 = hyequipmentCommon.GetAllFamilyInstance(document, BuiltInCategory.OST_MechanicalEquipment);
								new List<ElementId>();
								foreach (ElementId item in allFamilyInstance.Intersect(allFamilyInstance2))
								{
									allFamilyInstance2.Remove(item);
								}
								transaction = new Transaction(document, "布置水箱");
								transaction.Start();
								foreach (ElementId elementId in allFamilyInstance2)
								{
									Element elementById = document.GetElementById(elementId);
									if (double.Parse(currentTankPara.width) > 0.0 && elementById.GetParameter("宽度") != null)
									{
										elementById.GetParameter("宽度").Set(double.Parse(currentTankPara.width) / 304.8);
									}
									if (double.Parse(currentTankPara.height) > 0.0 && elementById.GetParameter("高度") != null)
									{
										elementById.GetParameter("高度").Set(double.Parse(currentTankPara.height) / 304.8);
									}
									if (double.Parse(currentTankPara.length) > 0.0 && elementById.GetParameter("长度") != null)
									{
										elementById.GetParameter("长度").Set(double.Parse(currentTankPara.length) / 304.8);
									}
									if (double.Parse(currentTankPara.pipein) > 0.0 && elementById.GetParameter("进水管管径") != null)
									{
										elementById.GetParameter("进水管管径").Set(double.Parse(currentTankPara.pipein) / 304.8);
									}
									if (double.Parse(currentTankPara.pipeout) > 0.0 && elementById.GetParameter("出水管管径") != null)
									{
										elementById.GetParameter("出水管管径").Set(double.Parse(currentTankPara.pipeout) / 304.8);
									}
									if (double.Parse(currentTankPara.pipeoverflow) > 0.0 && elementById.GetParameter("溢流管管径") != null)
									{
										elementById.GetParameter("溢流管管径").Set(double.Parse(currentTankPara.pipeoverflow) / 304.8);
									}
									if (double.Parse(currentTankPara.pipeblowoff) > 0.0 && elementById.GetParameter("泄水管管径") != null)
									{
										elementById.GetParameter("泄水管管径").Set(double.Parse(currentTankPara.pipeblowoff) / 304.8);
									}
									if (double.Parse(currentTankPara.volume) > 0.0 && elementById.GetParameter("公称容积") != null)
									{
										elementById.GetParameter("公称容积").Set(UnitConvert.CovertToAPI(double.Parse(currentTankPara.volume), (Autodesk.Revit.DB.DisplayUnitType)14));
									}
									if (double.Parse(currentTankPara.weight) > 0.0 && elementById.GetParameter("水箱重量") != null)
									{
										elementById.GetParameter("水箱重量").Set(UnitConvert.CovertToAPI(double.Parse(currentTankPara.weight), (Autodesk.Revit.DB.DisplayUnitType)189));
									}
									SetParaForInstance.SetOffsetForInstance(elementById, formLayWaterTankNew.offset, document);
								}
								document.Regenerate();
								transaction.Commit();
							}
						}
						else if (dialogResult == DialogResult.Cancel)
						{
							break;
						}
					}
					result = Autodesk.Revit.UI.Result.Succeeded;
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				message = ex.Message;
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				if (transaction != null && (int)transaction.GetStatus() != 3)
				{
					transaction.RollBack();
				}
			}
			return result;
		}
	}
}
